package pack

import (
	"mall_frontend_api/biz/model"
	"mall_frontend_api/biz/model/user_address"
)

func UserAddress(data *model.UserAddress, regionMap map[int64]*model.Region) *user_address.UserAddress {
	info := user_address.NewUserAddress()
	if data == nil {
		return nil
	}
	info.ID = data.ID
	info.ReceiverAddress = data.ReceiverAddress
	info.ReceiverRegionID = data.ReceiverRegionID
	info.ReceiverCityID = data.ReceiverCityID
	info.ReceiverProvinceID = data.ReceiverProvinceID
	info.ReceiverName = data.ReceiverName
	info.ReceiverMobile = data.ReceiverMobile
	info.ReceiverEmail = data.ReceiverEmail
	if d, ok := regionMap[data.ReceiverRegionID]; ok {
		info.ReceiverRegion = d.Name
	}
	if d, ok := regionMap[data.ReceiverCityID]; ok {
		info.ReceiverCity = d.Name
	}
	if d, ok := regionMap[data.ReceiverProvinceID]; ok {
		info.ReceiverProvince = d.Name
	}

	return info
}

func UserAddressList(data []*model.UserAddress, regionMap map[int64]*model.Region, defaultInfo *model.UserAddress) []*user_address.UserAddress {
	list := make([]*user_address.UserAddress, 0)
	if len(data) == 0 {
		return list
	}
	for _, dd := range data {
		if defaultInfo != nil && defaultInfo.ID == dd.ID {
			continue
		}
		list = append(list, UserAddress(dd, regionMap))
	}
	return list
}
